"""会话相关的数据模式

定义会话创建、更新、响应等数据结构
使用Pydantic进行数据验证和序列化
"""

from __future__ import annotations
from typing import Optional, List
from pydantic import BaseModel, Field, field_validator, ConfigDict
from datetime import datetime


class SessionCreateRequest(BaseModel):
    """会话创建请求模式"""
    
    room_id: str = Field(..., description="房间ID", min_length=1, max_length=36)
    user_id: Optional[str] = Field(None, description="用户ID（可选）", max_length=50)
    
    @field_validator('room_id')
    @classmethod
    def validate_room_id(cls, v):
        if not v or not v.strip():
            raise ValueError('房间ID不能为空')
        return v.strip()
    
    model_config = ConfigDict(
        json_schema_extra={
            "example": {
                "room_id": "room_12345",
                "user_id": "user_67890"
            }
        }
    )


class SessionUpdateRequest(BaseModel):
    """会话更新请求模式"""
    
    user_id: Optional[str] = Field(None, description="用户ID", max_length=50)
    
    model_config = ConfigDict(
        json_schema_extra={
            "example": {
                "user_id": "user_67890"
            }
        }
    )


class SessionResponse(BaseModel):
    """会话响应模式"""
    
    id: int = Field(..., description="主键ID，唯一标识一次记录（自增整型）")
    room_id: str = Field(..., description="房间ID")
    user_id: Optional[str] = Field(None, description="用户ID")
    start_time: Optional[str] = Field(None, description="开始时间")
    end_time: Optional[str] = Field(None, description="结束时间")
    total_seconds: int = Field(..., description="总时长（秒）")
    created_at: Optional[str] = Field(None, description="创建时间")
    
    model_config = ConfigDict(
        json_schema_extra={
            "example": {
                "id": 1,
                "room_id": "room_12345",
                "user_id": "user_67890",
                "start_time": "2024-01-15T10:30:00",
                "end_time": "2024-01-15T11:30:00",
                "total_seconds": 3600,
                "created_at": "2024-01-15T10:30:00"
            }
        }
    )


class SessionListResponse(BaseModel):
    """会话列表响应模式"""
    
    sessions: List[SessionResponse] = Field(..., description="会话列表")
    total: int = Field(..., description="总数量")
    
    model_config = ConfigDict(
        json_schema_extra={
            "example": {
                "sessions": [
                    {
                        "id": 1,
                        "room_id": "room_12345",
                        "user_id": "user_67890",
                        "start_time": "2024-01-15T10:30:00",
                        "end_time": None,
                        "total_seconds": 0,
                        "created_at": "2024-01-15T10:30:00"
                    }
                ],
                "total": 1
            }
        }
    )